package business;

import entity.Event;
import entity.Notification;
import entity.NotificationPK;
import entity.NotificationType;
import entity.User;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Stateless;

@Stateless
public class Notifier {

    @EJB
    private NotificationManager notificationManager;

    public void sendNotifications(Event event, NotificationType notificationType) {
        Date now = new Date();
        for (User participant : event.getParticipants()) {
            NotificationPK key = new NotificationPK(event.getEventId(), participant.getUsername(), now);
            Notification notification = new Notification(key);
            notification.setNotificationType(notificationType.toString());
            if (notificationType == NotificationType.MODIFICATION) {
                notification.setTitle("Event Modified");
                notification.setText("This event has been modified. Visit the event page for more details.");
            }
            else if (notificationType == NotificationType.DELETION) {
                notification.setTitle("Event Deleted");
                notification.setText("This event has been deleted. You can no more participate.");
            }
            notificationManager.create(notification);
        }
    }
}
